<template>
    <el-container style="height: 750px; border: 1px solid #eee">
        <div id="logo">
            后台管理系统
        </div>
        <el-aside width="200px" style="margin-top:77px">
            <!-- 把动态加载菜单提取成vue组件 -->
            <el-menu :default-openeds="[1]" background-color="rgb(57,61,73)" text-color="white" :unique-opened="true"
                router>
                <template v-for="item in mainObj.menus">
                    <el-sub-menu v-if="item.children && item.children.length > 0" :index="item.mid" :key="item.mid">
                        <template #title>
                            <el-icon>
                                <component :is="item.icon"></component>
                            </el-icon>
                            {{ item.mname }}
                        </template>
                        <el-menu-item v-for="child in item.children" :index="child.route" :key="child.mid">
                            <el-icon>
                                <component :is="child.icon"></component>
                            </el-icon>
                            {{ child.mname }}</el-menu-item>
                    </el-sub-menu>
                    <el-menu-item v-else :index="item.mid" :key="item.mid" :route="item.route">
                        <el-icon>
                            <component :is="item.icon"></component>
                        </el-icon>
                        {{ item.mname }}</el-menu-item>
                </template>
            </el-menu>
        </el-aside>
        <el-container>
            <el-header style="text-align: right; font-size: 12px;height: 77px">
                <div class="toolbar">
                    <el-dropdown>
                        <el-icon style="margin-right: 8px; margin-top: 33px">
                            <setting />
                        </el-icon>
                        <template #dropdown>
                            <el-dropdown-menu>
                                <el-dropdown-item>编辑个人信息</el-dropdown-item>
                                <el-dropdown-item>修改头像</el-dropdown-item>
                                <el-dropdown-item @click="logOut()">退出</el-dropdown-item>
                            </el-dropdown-menu>
                        </template>
                    </el-dropdown>
                    <span>{{ store.userinfo.username }}</span>
                    <el-avatar :size="mainObj.avatar.size" :src="store.userinfo.imgPath" id="userImage"></el-avatar>
                </div>
            </el-header>
            <el-main>
                <!-- <el-scrollbar>
                    <el-table :data="tableData">
                        <el-table-column prop="date" label="Date" width="140" />
                        <el-table-column prop="name" label="Name" width="120" />
                        <el-table-column prop="address" label="Address" />
                    </el-table>
                </el-scrollbar> -->
                <router-view />
            </el-main>
        </el-container>
    </el-container>
</template>
<script lang="ts" setup>
import axios from 'axios';
import request from '../utils/request';
import { reactive, ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
const router = useRouter();
import MainFrameApi from '../api/MainFrameApi';
//引入useCounterStore实例
import { useUserStore } from '../stores/userStore'
//创建stote实例
let store = useUserStore();

//绑定登录用户信息
var mainObj = reactive({
    menus: [] as any, //存放后端查询菜单数据
    avatar: {
        size: 40, //设置登录用户头像大小
    }
});
//发送同步请求
onMounted(async () => {
    //发送请求查询菜单
    let res = await MainFrameApi.getMenu(store.userinfo.username);
    if (res.data.success) {
        mainObj.menus = res.data.data;
    }
})

//退出登录
const logOut = () => {
    store.LogOut();
    router.push('/');
}

</script>

<style>
.el-header {
    background-color: rgb(26, 160, 148);
    color: #333;
    line-height: 77px;
}

.el-aside {
    color: #333;
    background-color: rgb(57, 61, 73);
}

.transition-box {
    margin-bottom: 10px;
    width: 800px;
    height: 500px;
    border-radius: 4px;
    background-color: #409EFF;
    text-align: center;
    color: #fff;
    padding: 40px 20px;
    box-sizing: border-box;
    margin-right: 20px;
    position: relative;
    top: 20px;

}

#logo {
    background-color: red;
    width: 200px;
    height: 77px;
    position: absolute;
    background-color: rgb(36, 51, 70);
    color: white;
    font-size: 20px;
    text-align: center;
    line-height: 77px;
    letter-spacing: 6px;

}

#userImage {
    margin-left: 5px;
    vertical-align: middle;
}
</style>